﻿Public Class frmRelance
    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim nCodeEtat As Integer
    Dim nTypeRelance As Integer
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmRelance_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        dDEBUT.Value = Now.AddDays(-30)
        dFIN.Value = Now
        ' par defaut relance commerciale
        CB_TYPES.SelectedIndex = 0
        ' effacer la listview
        LV_ELEVES.Items.Clear()
        TE_NB.Clear()

    End Sub

    Private Sub InitListeEleves(ByVal ix As Integer)
        Dim nb As Integer = 0
        Dim solde As Decimal

        ' effacer la listview
        LV_ELEVES.Items.Clear()
        LV_ELEVES.ListViewItemSorter = Nothing

        ' extraire les éleves d'après les critères
        szSQL = "CALL spGet_ELEVES_byRELANCE('" & CB_TYPES.SelectedItem.ToString & "'," & gUserAgence & ",'" & Format(dDEBUT.Value, "yyyy/MM/dd") & "','" & Format(dFIN.Value, "yyyy/MM/dd") & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        Do While Not rs.EOF
            LV_ELEVES.Items.Add(rs.Fields("cd_cli").Value)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("civilite").Value)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("nompren").Value)
            LV_ELEVES.Items(nb).SubItems.Add(nv(rs.Fields("formule").Value))
            ' Solde réel = total crédit – total débit + avance heures + Avance présentations
            solde = rs.Fields("credit").Value + rs.Fields("encours_societe").Value - rs.Fields("mtval").Value
            LV_ELEVES.Items(nb).SubItems.Add(solde)
            LV_ELEVES.Items(nb).SubItems.Add(rs.Fields("cree_le").Value)
            If solde < 0 Then
                LV_ELEVES.Items(nb).ForeColor = Color.Red
            End If
            nb = nb + 1
            rs.MoveNext()
        Loop
        rs.Close()
        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            SortListView(LV_ELEVES, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_ELEVES)

        ' nombre de fiches extraites
        TE_NB.Text = nb.ToString
        If nb = 0 Then
            BO_IMPRIMER.Visible = False
        Else
            BO_IMPRIMER.Visible = True
        End If

    End Sub
    Private Sub BO_EXTRAIRE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_EXTRAIRE.Click

        Select Case CB_TYPES.SelectedIndex
            Case 0 ' Commercial
                nCodeEtat = 23
                nTypeRelance = 1
                lb_DOSSIERS.Text = "Dossiers inscrits du"

            Case 1 ' Débiteurs
                nCodeEtat = 24
                nTypeRelance = 5
                lb_DOSSIERS.Text = "Dossiers actifs du"

            Case Else ' Incomplet
                nCodeEtat = 25
                nTypeRelance = 7

        End Select
        ' reafficher la liste
        InitListeEleves(0)

    End Sub

    Private Sub BO_IMPRIMER_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BO_IMPRIMER.Click

        ' effecer la table provisoire
        szSQL = "CALL spDEL_T_G_LIVRE('" & gUserCode & "')"
        ExecQuery(ctDSN_AEM, szSQL)

        ' remplir la table pour édition
        For i = 0 To LV_ELEVES.Items.Count - 1
            szSQL = "CALL spINS_T_RELANCE('" & gUserCode & "'," & gUserAgence & "," & LV_ELEVES.Items.Item(i).Text() & ",'Relance " & CB_TYPES.SelectedItem & "')"
            ExecQuery(ctDSN_AEM, szSQL)
        Next

        ' imprimer la relance
        FrmViewReport.pCodeEtat = nCodeEtat
        FrmViewReport.pParamRPT = ""
        FrmViewReport.ShowDialog()

    End Sub

    Private Sub LV_ELEVES_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_ELEVES.ColumnClick
        SortListView(LV_ELEVES, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_ELEVES_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_ELEVES.DoubleClick
        If LV_ELEVES.SelectedItems.Count > 0 Then
            gCD_CLI = LV_ELEVES.SelectedItems(0).Text
            ' afficher les détails 
            frmElevesMAJ.ShowDialog()
            ' réaffiche la viewlist
            ' InitListeEleves(LV_ELEVES.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_ELEVES_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_ELEVES.KeyDown
        If e.KeyCode = Keys.Enter Then
            ' Enter = DoubleClick
            LV_ELEVES_DoubleClick(sender, e)
        End If
        If e.KeyCode = Keys.Delete Then
            For Each lvItem As ListViewItem In LV_ELEVES.SelectedItems
                lvItem.Remove()
            Next
        End If
    End Sub

    Private Sub dFIN_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dFIN.ValueChanged
        If DateDiff(DateInterval.Day, dFIN.Value, dDEBUT.Value) > 0 Then
            MsgBox("La date de fin ne peut être plus grand que la date de début.", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub CB_TYPES_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles CB_TYPES.SelectedIndexChanged
        ' type de relance = Débiteurs ?
        If CB_TYPES.SelectedIndex = 1 Then
            dDEBUT.Enabled = False
            dFIN.Enabled = False
        Else
            dDEBUT.Enabled = True
            dFIN.Enabled = True
        End If
    End Sub
End Class